Photo by Luca Bravo on Unsplash
Today I started a 100 Day challenge to learn about new things.
See my Log book for more information about my challenge.
The first topic I want to learn about is the idea of Function as a Service (FaaS) and to start off on this journey I will be working through Alex Ellis’ Serverless for Everyone Else book.
Next I will go over the steps I took to get my first function deployed on my Mac.
Installing Multipass
Follow the instructions at: https://multipass.run/ for macOS
-
Download and run the installer as an Admin user
-
Spin up an Ubuntu instance, test and delete
Spin up (this took about 5 minutes the first time to get the latest Ubuntu LTS image)
$ multipass launch --name test Launched: test
Test a command
$ multipass exec test -- lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.2 LTS Release: 20.04 Codename: focal
List instances
$ multipass list Name State IPv4 Image test Running 192.168.64.2 Ubuntu 20.04 LTS
Stop and clean up $ multipass stop test $ multipass delete test $ multipass purge
Installing faasd
-
Create a new Ubuntu instance using
multipass
2 cores and 2 gigs of RAM
$ multipass launch --name faasd --cpus 2 --mem 2G Launched: faasd
-
Open a bash shell on the Ubuntu instance
$ multipass exec faasd /bin/bash To run a command as administrator (user "root"), use "sudo <command>". See "man sudo_root" for details. ubuntu@faasd:~$
-
Copy your ssh public key over to the instance so we can run ssh sessions directly against the instance
NOTE: See the last section on how to create a SSH key if you don’t already have one.
$ cat ~/.ssh/id_rsa.pub | pbcopy [pbcopy copies to the clipboard on macOS] Edit the authorized ssh keys for the instance and add the id_rsa.pub value ubuntu@faasd:~$ nano ~/.ssh/authorized_keys
-
Test ssh to the instance. Open a new terminal.
Get the IP address of the instance
$ multipass info faasd Name: faasd State: Running IPv4: 192.168.64.4 Release: Ubuntu 20.04.2 LTS Image hash: c5f2f08c6a1a (Ubuntu 20.04 LTS) Load: 0.00 0.00 0.00 Disk usage: 1.2G out of 4.7G Memory usage: 148.6M out of 1.9G
SSH into the instance (note: ubuntu is the default username)
$ ssh ubuntu@192.168.64.4
In my case because I have multiple keys for a number of services and because of the way my ~/.ssh/config file is setup, I need to explicitly state to use
PreferredAuthentications=publickey
$ ssh -i ~/.ssh/id_rsa -o PreferredAuthentications=publickey ubuntu@192.168.64.4
-
Now you have two ways to get a shell on the running instance
$ multipass exec faasd /bin/bash or $ ssh ubuntu@INSTANCE_IP
-
Install faasd on the instance
Drop to root user (just incase sudo fails along the way)
ubuntu@faasd:~$ sudo -i root@faasd:~#
Clone the repo
root@faasd:~# git clone https://github.com/openfaas/faasd --depth=1 Cloning into 'faasd'... ...
Install
root@faasd:~# cd faasd root@faasd:~# ./hack/install.sh ... Creating alias 'faas' for 'faas-cli'. ___ _____ ____ / _ \ _ __ ___ _ __ | ___|_ _ __ _/ ___| | | | | '_ \ / _ \ '_ \| |_ / _` |/ _` \___ \ | |_| | |_) | __/ | | | _| (_| | (_| |___) | \___/| .__/ \___|_| |_|_| \__,_|\__,_|____/ |_| CLI: commit: 6b5e7a14a598527063c7c05fb4187739b6eb6d38 version: 0.13.6
Verify faasd is running
root@faasd:~/faasd# systemctl status faasd ● faasd.service - faasd Loaded: loaded (/lib/systemd/system/faasd.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2021-03-08 20:25:14 GMT; 5min ago Main PID: 14201 (faasd) Tasks: 7 (limit: 2344) Memory: 37.8M (limit: 500.0M) CGroup: /system.slice/faasd.service └─14201 /usr/local/bin/faasd up
Deploying your first function
I will be using the UI (web portal) for exploring the faasd setup as well as deploy my first FaaS.
-
Get the admin user’s password
ubuntu@faasd:~$ sudo cat /var/lib/faasd/secrets/basic-auth-password ;echo zI24jRLe45DhUPa21S6BoNy5q1DpYUb0FuDwlLfUoBNo6yaDBMZEPTO2W3K
-
Open up the UI in your browser. E.g. http://INSTANCE_IP:8080
-
Provide username "admin" and the password from the previous steps
-
Select "Deploy New Function" and search for "ASCII Cows". Deploy it.
-
After a few seconds or so you will see COWS in the sidebar. Select it.
-
Invoke the function and you will get a random ascii cow
-
You can also run curl against the deployed function
$ curl http://192.168.64.4:8080/function/cows * (__) \ (OO) \-------\/ / \\ //------\\ ^^ ^^ Cow who's just seen a McDonalds
Miscellaneous
Creating a SSH key-pair
I like to have a SSH key-pair per machine or service I use and store a backup copy in my password manager (1Password). For this example I will just create the default id_rsa key pair that will also be used for connecting to the Ubuntu local instances.
Run the following command to create a 4kb RSA public and private key-pair. The filenames will be id_rsa (the private key, never ever share this one!) and id_rsa.pub (the public key)
$ ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa -C "Add a comment here if you want"
I use the password manager to generate a strong passphrase. To rule out any copy/paste and unrecoverable errors in the future, I like to copy/paste the stored secret from my recording (password manager entry) and then when asked to confirm, I manually type it in. This way I can be sure that what I copy/paste will always be correct and at the very least I would be able to manually type it in if the need ever presented itself.
Backup the generated key-pair (~/.ssh/id_rsa and ~/.ssh/id_rsa.pub) to your password manager. Tip: To see hidden files on macOS (in Finder or File dialogue) enter Cmd + Shift + .
(period for dot files).
Add the key to your macOS login keychain so you don’t have to always enter the passphrase.
$ ssh-add -K ~/.ssh/id_rsa
Hi ,
Cool! I also worked with Multipass on windows machine and virtualbox as VM driver. The installation via cloud-init worked very well. Only the access via ip address didn’t work well with my setup. I had to add some additional configs and port forwards. Luckily the Multipass documentation helped pretty good
https://multipass.run/docs/using-virtualbox-in-multipass-windows?&_ga=2.93498011.76072282.1615446925-1488768319.1615446925#port-forwarding
And I adapted the nextcloud configuration to the faasd ones
https://ubuntu.com/appliance/nextcloud/vm#windows
Faasd is great! Happy tinkering!